package org.fiel.admin.struts.actions;

import javax.servlet.http.*;
import org.apache.struts.action.*;
import org.fiel.admin.dao.UserDAO;
import org.fiel.admin.struts.forms.UserForm;

public final class AddUserAction extends AdminAction
{

    public AddUserAction()
    {
    }

    public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception
    {
        org.apache.struts.util.MessageResources messages = getResources(request);
        HttpSession session = request.getSession();
        ActionErrors errors = new ActionErrors();
        UserForm userForm = (UserForm) form;
        UserDAO uDAO = new UserDAO();
        try
        {
            if (!isAuthorized(request))
            {
                throw new Exception("Usuario no autorizado para esta operaci\363n.");
            }
            uDAO.createUser(userForm);
        }
        catch (Exception e)
        {
            System.out.println(e.toString());
            errors.add("org.apache.struts.action.GLOBAL_ERROR", new ActionError("error.exception"));
            errors.add("org.apache.struts.action.GLOBAL_ERROR", new ActionError("error.detail", e.getMessage()));
        }
        if (mapping.getAttribute() != null)
        {
            if ("request".equals(mapping.getScope()))
            {
                request.removeAttribute(mapping.getAttribute());
            }
            else
            {
                session.removeAttribute(mapping.getAttribute());
            }
        }
        if (!errors.isEmpty())
        {
            saveErrors(request, errors);
            return mapping.findForward("failed");
        }
        else
        {
            return mapping.findForward("success");
        }
    }
}
